/**
 * View.java
 * 
 * @authors: Connor Gordon, Difan Zhao, Kathleen Lan, Patrick Royal
 */

package view;

import control.Controller;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import model.Element;
import view.visualizer.BubbleDrawer;
import view.visualizer.ElementVisualDrawer;


/**
 * Main frame that display show several technology company stocks, the sizes of
 * the
 * bubble are proportional to the average price of its stock. When user clicks
 * on
 * one particular stock, a pop up window will show the price history for that
 * particular stock.
 * 
 * @author difan zhao, kathleen lan
 * 
 */
public class View extends JFrame {

    /**
     * Autogenerated version UID.
     */
    private static final long serialVersionUID = 1L;
    private static final int NUM_DATA_SETS = 3;
    private static final int CANVAS_WIDTH = 700;
    private static final int CANVAS_HEIGHT = 700;
    private ResourceBundle myResources;
    private ActionListener[] myActionListeners = new ActionListener[NUM_DATA_SETS];
    private String[] myDataSetNames = {"OnePeriod", "TwoPeriod", "ThreePeriod"};
    private MouseAdapter myMouseListener;
    private Controller myController;

    private ElementVisualDrawer myElementVisualDrawer;
    private DrawFactory myDrawFactory;

    private final JFileChooser myChooser = new JFileChooser(
            System.getProperties().getProperty("user.dir"));
    private String[] myExtensionPossibilities = {".csv", ".xls"};

    /**
     * Constructor.
     * 
     * @param title title of the frame
     * @param con controller that controls the view
     */
    public View (String title, Controller con) {
        myController = con;
        // set properties of frame
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(CANVAS_WIDTH, CANVAS_HEIGHT);

        // create and arrange sub-parts of the GUI
        // myResources = ResourceBundle.getBundle("resources.English");
        makeListeners();
        // getContentPane().add(makeButton(), BorderLayout.NORTH);
        setupElementVisualDrawer();
        myDrawFactory = new DrawFactory();
        // DrawFactory.setGraphStyle(1);
        // repaint();
        // size and display the GUI
        // pack();
    }

    /**
     * Sets up the ElementVisualDrawer.
     */
    private void setupElementVisualDrawer () {
        myElementVisualDrawer = new BubbleDrawer();
        myElementVisualDrawer.setCanvasSize(getWidth(), getHeight());
        getContentPane().add(myElementVisualDrawer, BorderLayout.CENTER);
        myElementVisualDrawer.addMouseListener(myMouseListener);
    }

    /**
     * Makes action listeners and mouse listeners.
     */
    protected void makeListeners () {
        // listener for "high-level" events, i.e., those made
        // up of a sequence of low-level events, like a button
        // press (mouse down and up within a button object)
        for (int k = 0; k < NUM_DATA_SETS; k++) {
            myActionListeners[k] = new ActionListener() {
                @Override
                public void actionPerformed (ActionEvent e) {
                    // myController.print();
                }
            };
        }

        myMouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked (MouseEvent e) {
                // myController.print();
                int x = e.getX();
                int y = e.getY();
                String stockName = myElementVisualDrawer.clickedElementVisualCheck(x, y);
                if (stockName != null) {
                    List<Integer> stockData = myController.getElementData(stockName);
                    myDrawFactory.getDrawGraph().createGraph(stockData,
                            stockName);
                    // DrawLineGraph.createAndShowGui(stockData);
                }
            }
        };
    }

    protected JComponent makeButton () {
        JPanel result = new JPanel();
        for (int k = 0; k < NUM_DATA_SETS; k++) {
            result.add(makeDataSetButton(myDataSetNames[k], k));
        }
        return result;
    }

    protected JComponent makeCanvas () {
        JPanel result = new JPanel();
        result.add(makeDataSetButton(myDataSetNames[0], 0));
        return result;
    }

    protected JButton makeDataSetButton (String s, int n) {
        JButton result = new JButton(myResources.getString(s));
        result.addActionListener(myActionListeners[n]);
        return result;
    }

    /**
     * Given a set of data, creates a visualization for them.
     * @param myElements dataset to visualize
     */
    public void createVisualization (List<Element> myElements) {
        myElementVisualDrawer.createVisualization(myElements);
    }

    /**
     * Shows an intro message in the popup window.
     */
    public void introMessage () {
        JOptionPane.showMessageDialog(this,
                "Greetings! In the next few windows, you will be prompted to \n " +
                        "1) select a file, \n" +
                        "2) enter a URL to retrieve data from, and \n" +
                        "3) enter an file extension type. \n" +
                        "Please click 'ok' to continue.");
    }

    /**
     * Prompt to user for input file.
     * 
     * @return file name
     */
    public String promptName () {
        int response = myChooser.showOpenDialog(null);
        if (response == JFileChooser.APPROVE_OPTION) {
            return myChooser.getSelectedFile().toString();
        }
        return null;
    }

    /**
     * propmt to user for url
     * 
     * @return url
     */
    public String promptURL () {
        String s = (String) JOptionPane.showInputDialog(
                        this,
                        "Enter the URL for the data source website.\n\n" +
                                "Two Good Options: \n " +
                                "1) Google Finance: www.google.com/finance   (.csv)\n" +
                                "2) Yahoo Finace: ichart.finance.yahoo.com        (.csv)\n\n",
                        "URL prompt", JOptionPane.PLAIN_MESSAGE);
        return s;
        /*
         * System.out.println("Enter URL here : ");
         * return promptForInput();
         */
    }

    /**
     * prompt to user for extension
     * 
     * @return extension
     */

    public String promptExt () {
        String s = (String) JOptionPane.showInputDialog(this,
                "Enter the extension type for the file to be retrieved " +
                        "from the data source website.", "Extension prompt",
                JOptionPane.PLAIN_MESSAGE, null, myExtensionPossibilities,
                myExtensionPossibilities[0]);
        return s;
        /*
         * System.out.println("Enter Ext here : ");
         * return promptForInput();
         */
    }

    /*
     * public String promptForInput(){
     * try {
     * BufferedReader bufferRead = new BufferedReader(
     * new InputStreamReader(System.in));
     * String s = bufferRead.readLine();
     * System.out.println(s);
     * return s;
     * }
     * catch (IOException e) {
     * e.printStackTrace();
     * }
     * return null;
     * }
     */

}
